package com.learning.spring.cloud.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/test")
public class TestEurekaController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/instances")

    public String getInstances() {
        List<ServiceInstance> instances = discoveryClient.getInstances("product-service");

        return instances
                .stream()
                .map((ServiceInstance u) -> "instanceId:%s, appName:%s, host:%s, port:%s".formatted(
                        u.getInstanceId(), u.getScheme(), u.getHost(), u.getPort()))
                .collect(Collectors.joining(";"));
    }
}
